(function rhTabModule(factory){
	// if (typeof define === "function" && define.amd)
	// 	define(factory)
	// else if (typeof module != "undefined" && module.exports != "undefined")
	// 	module.exports = factor()
	// else
		window['RhTab'] = factory()

})(function rhTabFactory (){

	if (typeof window == "undefined" || !window.document){
		return function rhTabError() {
			throw new Error("rhTab.js requires a window with a document")
		}
	}

	var c_ul_data_key = "label"
	var c_right_data_key = "id"


	function RhTab(paramEl ,paramOpt){
		this.el = paramEl
		this.opt = paramOpt
		this.el.addClass("um-tab")
		this.left_panel = $('<div class="__left_panel"></div>').appendTo(this.el)
		this.left_panel_ul = $('<ul class="__ul"></ul>').appendTo(this.left_panel)
		this.right_panel = $('<div class="__right_panel"></div>').appendTo(this.el)
	}

	RhTab.prototype = {
		init : function (initVal){
			var self = this

			self.__left_data_render()

			if (initVal)
				self.__init_val_render(initVal)
		},
		__left_data_render : function (){
			var self = this

			var ul_data = self.opt.ul_data
			for (var i = 0; i < ul_data.length; i++) {
				self.left_panel_ul.append('<li data-val="'+ul_data[i][c_ul_data_key]+'">'+ul_data[i][c_ul_data_key]+'</li>')
			}

			self.left_panel_ul.slimscroll({});

			self.left_panel_ul.find("li").click(function (){
				$(this).addClass("active").siblings().removeClass("active")
				self.__right_data_render($(this).html())
			})
		},
		__right_data_render : function (type){
			var self = this
			var list = self.opt.data[type]
			this.right_panel.empty()
			for (var i = 0; i < list.length; i++) {
				this.right_panel.append('<div class="__name" data-val="'+list[i][c_right_data_key]+'">'
													+list[i].label+'<i class="icon-ok"></i></div>')
			}

			this.right_panel.find("[class*=__name]").click(function (){
				$(this).addClass("active").siblings().removeClass("active")
				self.opt.right_click && self.opt.right_click($(this).attr("data-val"))
			})
		},
		__init_val_render : function (initVal){
			var self = this
			var data = self.opt.data

			for (key in data){
				var tmpArray = data[key]
				var tmp = _.find(tmpArray ,function (obj){
					return obj[c_right_data_key] == initVal
				})
				if (tmp)
				{
					self.left_panel_ul.find("[data-val="+key+"]").click()
					self.right_panel.find("[data-val="+tmp[c_right_data_key]+"]").addClass("active")
				}
			}
		},
		getSelectedId : function (){
			return this.right_panel.find("[class*=active]").attr("data-val")
		},
		getSelectedName : function (){
			return this.right_panel.find("[class*=active]").text()
		}
	}

	return RhTab
})